<template>
  <div class="contentRight2">
    <!--视频观看记录-->
    <div id="tools">

      <div class="height8"></div>
      <!-- 查询条件 -->
      <a-form layout="inline" style="text-align: left;padding: 0 10px;" :selfUpdate="true">

        <a-form-item label="开始日期" class="marginBtm">
          <a-date-picker
            :allowClear="false"
            :disabledDate="disabledStartDate"
            format="YYYY-MM-DD"
            v-model="startDate"
            placeholder="开始日期"
            style="width: 130px;margin-right: 5px;"
          />
        </a-form-item>
        <a-form-item label="结束日期" class="marginBtm">
          <a-date-picker
            :allowClear="false"
            :disabledDate="disabledEndDate"
            format="YYYY-MM-DD"
            placeholder="结束日期"
            v-model="endDate"
            style="width: 130px;margin-right: 5px;"
          />
        </a-form-item>

        <!--<a-form-item label="方向" class="marginBtm">-->
          <!--&lt;!&ndash;99全部1出2进&ndash;&gt;-->
          <!--<a-dropdown style="">-->
            <!--<a-menu slot="overlay" @click="handleMenuClick">-->
              <!--<a-menu-item :key="item.id" v-for="item in approvalStatus">{{item.name}}</a-menu-item>-->
            <!--</a-menu>-->
            <!--<a-button>{{ statusValue }}-->
              <!--<a-icon type="down"/>-->
            <!--</a-button>-->
          <!--</a-dropdown>-->
        <!--</a-form-item>-->

        <a-form-item label="关键字" class="marginBtm">
          <a-input v-model="queryValue" allowClear placeholder="请输入姓名/手机号" class="marginBtm"
                   @pressEnter="inquireStart"/>
        </a-form-item>

        <a-form-item class="marginBtm">
          <a-button type="primary" @click="inquireStart">
            查询
          </a-button>
        </a-form-item>

      </a-form>
      <a-divider style="margin: 5px 0"/>
      <div class="divPadiAlign" v-if="isShowItGroupButton == 1">
        <a-button type="primary" class="button-marin" @click="exportList">
          导出
        </a-button>
        <!--<a-button type="primary" class="button-marin" @click="addDilog">-->
        <!--新增-->
        <!--</a-button>-->
      </div>
    </div>

    <a-table
      class="tabled"
      bordered
      :fixed="true"
      size="small"
      :columns="columns"
      :row-key="record => Math.random()"
      :data-source="tableData"
      :pagination="pagination"
      :loading="loading"
      :scroll="{ x:xWidth,y: yHeight }"
      :locale="localezh_CN"
      @change="handleTableChange"
    >
      <!--点击行选中行，点击单选按钮选中行；但是需要:row-key是唯一标示字段-->
      <!--:rowSelection="{type:'radio',selectedRowKeys:selectedRowKeys,fixed:true,onChange:onChange}"-->
      <!--:customRow="clickRow"-->

      <!-- <template slot="name" slot-scope="name"> {{ name.first }} {{ name.last }} </template> -->
    </a-table>

    <a-modal v-model="visible" title="新增" @ok="handleOk">
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </a-modal>

    <!--导出加载中-->
    <customLoading :customVisible="spinning"></customLoading>

  </div>
</template>

<script>
  import {tableBoxHeight} from "@/api/communal";
  import reservationApi from "@/api/reservation";
  import adminsDefinitionApi from "@/api/visitorSystem/adminsDefinition";
  import localezh_CN from 'antd/lib/locale-provider/zh_CN';
  import dateUtil from "@/utils/dateUtils";
  import moment from "moment";
  import {ConfigProvider} from 'ant-design-vue';//引用
  import 'moment/locale/zh-cn';
  import cfg from '@/config/config'
  const Qs = require('qs');

  export default {
    components: {
      ConfigProvider
    },
    name: '',
    data() {
      return {
        localezh_CN,
        spinning: false,
        isShowItGroupButton: 0,  // 0 不展示导出等其它按钮，只展示查询  1 展示导出等其它按钮
        delayTime: 500,
        xWidth: "",
        yHeight: "",
        tableData: [],
        pagination: {
          pageSize: 30,
          current: 1,
          total: 0,
          showSizeChanger: true,
          // showQuickJumper: true,
          pageSizeOptions: ["20", "30", "50"],
          showTotal: (total) => `共 ${total} 条`, // 显示总数
          onShowSizeChange: (current, pageSize) => {
            // console.log("1111");
            this.pagination.current = 1;
            this.pagination.pageSize = pageSize;
          },
        },
        selectedRowKeys: [],
        loading: false,
        dateFormat: 'YYYY/MM/DD',
        startDate: "",
        endDate: "",
        endOpen: false,
        startDated: "",
        endDated: "",
        queryValue: "",  //查询值,访客名称，访客手机号
        columns: [],   //表格表头
        statusValue: "",  //状态名称
        statusId: 99,   //状态id，<!--99全部1出2进-->
        approvalStatus: [
          {name: "全部", id: 99},
          {name: "出", id: 1},
          {name: "进", id: 2},

        ],
        visible: false


      }
    },
    beforeMount() {
      // carDatetime: "2020-11-15 10:11:43"
      // carInout: "进"
      // carName: "沪BDS391"
      // carSbidName: "一期出口左侧"
      // userLname: null
      // userNo: null
      this.columns = [
        {
          title: '序号',     //表头
          dataIndex: 'serialNumber',   //字段名称
          width: '50px',    //设列宽度
          align: 'center',  //居中显示
          fixed: "left",
          className: 'changeBack',   //重置覆盖背景色为浅灰色，否则是白色的
          customRender: (value, row, index) => `${(this.pagination.current - 1) * this.pagination.pageSize + index + 1}`  //设置动态序号
        },
        {
          title: '姓名',
          dataIndex: 'vistorName',
          width: '100px',
          align: 'center',
          ellipsis: true,
          className: 'changeBack'
        },
        {
          title: '手机号码',
          dataIndex: 'vistorTel',
          width: '100px',
          align: 'center',
          ellipsis: true,
          className: 'changeBack'
        },
        {
          title: '有效期开始时间',
          dataIndex: 'isValidBegin',
          width: '150px',
          minWidth: '150px',
          align: 'center',
          className: 'changeBack',
          ellipsis: true,   //此属性可以让单元格内容根据宽度自动省略
        },
        {
          title: '有效期结束时间',
          dataIndex: 'isValidEnd',
          width: '150px',
          minWidth: '150px',
          align: 'center',
          className: 'changeBack',
          ellipsis: true,   //此属性可以让单元格内容根据宽度自动省略
        },
        {
          title: '观看视频开始时间',
          dataIndex: 'beginTime',
          width: '150px',
          minWidth: '150px',
          align: 'center',
          className: 'changeBack',
          ellipsis: true,   //此属性可以让单元格内容根据宽度自动省略
        },
        {
          title: '观看视频结束时间',
          dataIndex: 'endTime',
          width: '150px',
          align: 'center',
          ellipsis: true,
          className: 'changeBack',
        },
        {
          title: '',
          dataIndex: '',
          width: 'auto',
          align: 'center',
          className: 'changeBack',
        },

      ];
      // this.tableData =[
      //   {userNo:12213,userLname:"张四",carName:"F12312",carSbidName:"2131",carInout:"进",visitDateBegin:"",carDatetime:""}
      // ]
      this.startDate = moment().startOf('month');
      this.endDate = moment().endOf('month');
    },
    mounted() {
      // console.log(tableBoxHeight());
      this.xWidth = this.getTableWidth();
      // this.statusValue = this.approvalStatus[0].name;
      // this.statusId = this.approvalStatus[0].id;
      this.checkIfItGroup();
      this.inquireStart();


    },
    methods: {
      //验证当前登录人是不是IT管理员组
      checkIfItGroup() {
        adminsDefinitionApi.checkIfItGroup().then(res=>{
          console.log(res.data);
          if (res.data.success) {
            // 0 不展示导出等其它按钮，只展示查询  1 展示导出等其它按钮
            this.isShowItGroupButton = res.data.result.isShow;
            this.$nextTick(()=>{
              this.yHeight = tableBoxHeight() + 3 + "px";
              let minHeight = document.getElementsByClassName("ant-table");
              minHeight[0].style.minHeight = tableBoxHeight() + 40 + "px";
            })
          } else {
            this.$message.error(res.data.message);
          }
        })
      },
      // 计算表格宽度
      getTableWidth() {
        let totals = null;
        let el;
        for (el of this.columns) {
          if (el.width != 'auto') {
            totals += parseInt(el.width);
          }
        }
        return totals + "px"
      },
      // 选择日期
      onChangePicker(date, dateString) {
        // console.log(date, dateString);
        this.startDated = dateString[0];
        this.endDated = dateString[1];
      },
      //日期控件需要的方法
      disabledStartDate(startDate) {
        const endDate = this.endDate;
        if (!startDate || !endDate) {
          return false;
        }
        return startDate.valueOf() > endDate.valueOf();
      },
      disabledEndDate(endDate) {
        const startDate = this.startDate;
        if (!endDate || !startDate) {
          return false;
        }
        return startDate.valueOf() >= endDate.valueOf();
      },
      handleStartOpenChange(open) {
        // if (!open) {
        //   this.endOpen = true;
        // }
      },
      handleEndOpenChange(open) {
        this.endOpen = open;
      },

      // 导出列表
      async exportList() {
        if (!this.verification()) return;
        this.spinning = true;
        let param = {
          current: this.pagination.current,
          pageSize: this.pagination.pageSize,
          startDate: this.startDated,
          endDate: this.endDated,
          // carInout: this.statusId,
          serchTxt: this.queryValue
        };
        let jsonIn = encodeURI(encodeURI(JSON.stringify(param)));
        await window.open(cfg.apiUrl + 'api/visitVideo/visitVideoWatchExport?jsonIn=' + jsonIn, "_self");

        // let  jsonIn2 = Qs.stringify(param);
        // window.open(cfg.apiUrl + 'api/visitVideo/visitVideoWatchExport?jsonIn=' + jsonIn2, "_self");
        setTimeout(()=>{
          this.spinning = false;
        },1000)

      },
      verification() {
        if (!this.startDate) {
          this.startDated = "";
        } else {
          this.startDated = moment(this.startDate).format("YYYY-MM-DD") ;
        }
        if (!this.endDate) {
          this.endDated = ""
        } else {
          this.endDated = moment(this.endDate).format("YYYY-MM-DD") ;
        }
        return true
      },
      // 开始查询
      inquireStart() {
        if (!this.verification()) return;
        this.pagination.current = 1;
        let param = {
          current: this.pagination.current,
          pageSize: this.pagination.pageSize,
          startDate: this.startDated,
          endDate: this.endDated,
          // carInout: this.statusId,   //carInout
          serchTxt: this.queryValue
        };
        // console.log(param);
        this.fetch(param);
      },
      // 审批状态下拉菜单
      handleMenuClick(e) {
        // console.log(e);
        this.approvalStatus.filter(el => {
          if (el.id == e.key) {
            // console.log(el);
            this.statusValue = el.name;
            this.statusId = el.id
          }
        });
        // console.log(this.statusValue,this.statusId);
      },
      // 点击行
      clickRow(record) {
        return {
          on: {
            click: () => {
              console.log(record)
              let keys = [];
              keys.push(record.orderId);
              this.selectedRowKeys = keys;
            }
          }
        }

      },
      // 表格切换分页
      handleTableChange(pagination, filters, sorter) {
        if (!this.verification()) return;
        const pager = {...this.pagination};
        pager.current = pagination.current;
        this.pagination = pager;
        this.fetch({
          pageSize: pagination.pageSize,
          current: pagination.current,
          startDate: this.startDated,
          endDate: this.endDated,
          // approvalStatus: this.statusId,
          serchTxt: this.queryValue,
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...filters,
        });
      },
      addDilog() {
        this.visible = true;
      },
      handleOk() {
        console.log("点击了ok");
        this.visible = false;
      },

      onChange(selectedRowKeys, selectedRows) {
        // console.log(selectedRowKeys, selectedRows);
        this.selectedRowKeys = selectedRowKeys;
      },
      // 请求表格数据--测试用
      async fetch(params) {
        this.loading = true;
        let resp = await reservationApi.findVisitWatchLogList(params);
        console.log(resp.data);
        if (resp.data.success) {
          // console.log("111");
          this.loading = false;
          this.pagination.total = resp.data.result.total;
          this.tableData = resp.data.result.records;

        } else {
          // console.log("222");
          this.loading = false;
          this.$message.error(resp.data.message);
        }

        // 测试
        // setTimeout(() => {
        //   this.tableData=[
        //     {serialNumber:"0",oddNumber:"14",visitName:"王晓明",visitCompony:"上海步达",visitTel:"13345674568",vistorCertNumber:"410526194601114235",userLname:"冯伟",userTelephone:"17612345678",visitDateBegin:"2020-10-10 08:00:00",visitDateEnd:"2020-10-10 17:30:00",entourage:"主访客",approvalStatus:"已通过"},
        //     {serialNumber:"0",oddNumber:"14",visitName:"王晓明",visitCompony:"上海步达",visitTel:"13345674568",vistorCertNumber:"410526194601114235",userLname:"冯伟",userTelephone:"17612345678",visitDateBegin:"2020-10-10 08:00:00",visitDateEnd:"2020-10-10 17:30:00",entourage:"主访客",approvalStatus:"已通过"},
        //   ];
        // this.tableData=this.tableData.map((el,i)=>{
        //    el.serialNumber = i + 1;
        //    return el
        // });
        // console.log(this.tableData);
        //   this.loading = false;
        // }, 2000);
      },


    },
  }
</script>

<style scoped>

</style>

